package com.pkk.penguin.frame.know.java.Javacoreknowledge.thread.threadsimple;

import org.junit.Test;

/**
 * @description: 线程中断标志
 * @author: peikunkun
 * @create: 2019-12-26 17:44
 **/
public class ThreadInterrupted extends Thread {

  /**
   * 是否睡眠
   */
  public static volatile boolean isSleep = false;


  /**
   * If this thread was constructed using a separate
   * <code>Runnable</code> run object, then that
   * <code>Runnable</code> object's <code>run</code> method is called;
   * otherwise, this method does nothing and returns.
   * <p>
   * Subclasses of <code>Thread</code> should override this method.
   *
   * @see #start()
   * @see #stop()
   */
  @Override
  public void run() {
    try {
      if (isSleep) {
        Thread.sleep(3000L);
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }


  @Test
  public void Administrator_34_20191227145437() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_34()_20191227145437】");
    System.out.println("此方法测试描述：【】");
    ThreadInterrupted threadInterrupted = new ThreadInterrupted();
    System.out.println("是否中断:" + threadInterrupted.isInterrupted());
    isSleep = true;
    threadInterrupted.start();
    System.out.println("是否中断:" + threadInterrupted.isInterrupted());
    threadInterrupted.join();
    System.out.println("是否中断:" + threadInterrupted.isInterrupted());
  }


  @Test
  public void Administrator_31_20191227102819() {
    System.out.println("欢迎使用单元测试方法【Administrator_31()_20191227102819】");
    System.out.println("此方法测试描述：【】");
    ThreadInterrupted threadInterrupted = new ThreadInterrupted();
    threadInterrupted.start();
    //中断
    threadInterrupted.interrupt();
    System.out.println("是否中断" + threadInterrupted.isInterrupted());
    System.out.println("是否中断" + threadInterrupted.isInterrupted());
    System.out.println("是否存活" + threadInterrupted.isAlive());
    //清除当前线程的标记
    System.out.println("线程标记清清除" + interrupted());
    System.out.println("线程标记清清除" + interrupted());
    System.out.println("当前线程:" + Thread.currentThread().getName());
    System.out.println("threadInterrupted线程:" + threadInterrupted.getName());
    System.out.println("是否中断" + threadInterrupted.isInterrupted());
    System.out.println("是否中断" + threadInterrupted.isInterrupted());
    System.out.println("是否存活" + threadInterrupted.isAlive());
    System.out.println("守护线程" + threadInterrupted.isDaemon());


  }


  /**
   * @Description: 基础的使用方法
   * @return: void
   * @Author: peikunkun
   * @Date: 2019/12/27 0027 上午 10:27
   */
  @Test
  public void Administrator_27_20191226174548() {
    System.out.println("欢迎使用单元测试方法【Administrator_27()_20191226174548】");
    System.out.println("此方法测试描述：【】");
    final ThreadInterrupted threadInterrupted = new ThreadInterrupted();
    //中断线程【threadInterrupted】此中断只是设置一个中断标识,线程仍然会继续运行
    threadInterrupted.interrupt();

    //测试当前线程是否被中断,返回boolean并清除中断状态,第二次再调用时中断状态已经被清除，将返回一个false
    interrupted();

    //测试线程【threadInterrupted】中断状态
    threadInterrupted.isInterrupted();


  }

}
